package com.eeprint.system.service.tenant.impl;



import com.eeprint.system.domain.tenant.TenantPrinter;
import com.eeprint.system.domain.tenant.vo.TenantPrinterMessage;
import com.eeprint.system.domain.tenant.vo.TerminalWithPrinterVo;
import com.eeprint.system.domain.terminal.entity.TenantTerminal;
import com.eeprint.system.mapper.tenant.TenantPrinterMapper;
import com.eeprint.system.mapper.terminal.TerminalMapper;
import com.eeprint.system.service.tenant.TenantPrinterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class TenantPrinterServiceImpl implements TenantPrinterService {
    @Autowired
    TenantPrinterMapper tenantPrinterMapper;

    @Autowired
    TerminalMapper terminalMapper;

    /**
     * @Author: wsen on 2020/10/18 20:27
     * @Description: 查询预打印的设备
     */
    @Override
    public List<TerminalWithPrinterVo> findPrePrinter(BigInteger relTenantId) {
        /** 找出该租户下的所有打印机设备 */
        List<TenantPrinter> printerList = tenantPrinterMapper.selectByRelTenantId(relTenantId);
        /** 找出每台打印机对应的终端 */
        List<TerminalWithPrinterVo> terminalWithPrinterVos = printerList.stream().map(p -> {
            /** 生成TerminalWithPrinterVo对象 */
            TerminalWithPrinterVo terminalWithPrinterVo = new TerminalWithPrinterVo();
            /** 对打印机信息进行赋值 */
            terminalWithPrinterVo.setPrinterId(p.getId());
            terminalWithPrinterVo.setPrinterName(p.getPrinterName());
            /** 根据终端id找出终端名字 */
            TenantTerminal tenantTerminal = terminalMapper.selectById(p.getRelTerminalId());
            /** 对终端信息进行赋值 */
            terminalWithPrinterVo.setTerminalId(tenantTerminal.getId());
            terminalWithPrinterVo.setTerminalName(tenantTerminal.getTerminalName());
            return terminalWithPrinterVo;
        }).collect(Collectors.toList());
        return terminalWithPrinterVos;
    }

    /**
     * @Author: wsen on 2020/10/18 23:13
     * @Description: 获取打印机参数
     */
    @Override
    public TenantPrinter getPrinterParam(BigInteger id) {
        return tenantPrinterMapper.selectById(id);
    }

    /**
     * @Author: wsen on 2020/10/19 16:29
     * @Description: 查看打印机列表
     */
    @Override
    public List<TenantPrinterMessage> list(BigInteger relTenantId) {
        List<TenantPrinter> tenantPrinters = tenantPrinterMapper.selectByRelTenantId(relTenantId);
        List<TenantPrinterMessage> collect = tenantPrinters.stream().map(p -> {
            String terminalName = terminalMapper.selectNameById(p.getRelTerminalId());
            TenantPrinterMessage printerMessage = new TenantPrinterMessage(
                    p.getId(), terminalName, p.getPrinterName(), p.getPrinterDriver(),
                    p.getPrinterPaperSuport(), p.getPrinterColorSuport(), p.getPrinterDuplexSuport(),
                    p.getPrinterIsUsed(), p.getPrinterStatus()
            );
            return printerMessage;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * @Author: wsen on 2020/10/19 17:01
     * @Description: 根据id查看打印设备详情
     */
    @Override
    public TenantPrinter findPrinter(BigInteger id) {
        return tenantPrinterMapper.selectById(id);
    }

    @Override
    public int editParam(TenantPrinter tenantPrinter) {
        return tenantPrinterMapper.update(tenantPrinter);
    }
}
